
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>MySQL锁 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>MySQL锁 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">行锁（Row Lock）相比于其他锁类型有哪些主要的优势和劣势？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">行锁的优势在于：锁的粒度小，发生锁冲突的概率低，以及处理并发的能力强。其劣势在于：开销大，加锁慢，并且可能会出现死锁。</div>
          </div>
          <div class="card-source">来源: 行锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">InnoDB存储引擎是如何自动为不同的SQL语句加锁的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">对于UPDATE、DELETE和INSERT语句，InnoDB会自动为所涉及的数据集加上排他锁。而对于普通的SELECT语句，InnoDB则不会施加任何锁。</div>
          </div>
          <div class="card-source">来源: 行锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">在InnoDB中，如何通过SELECT语句显式地为数据行添加共享锁和排他锁？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">添加共享锁的语法是在SELECT语句后附加 `lock in share more`。添加排他锁的语法是在SELECT语句后附加 `for update`。</div>
          </div>
          <div class="card-source">来源: 行锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">表锁（Table Lock）相比于其他锁类型有哪些主要的优势和劣势？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">表锁的优势在于：开销小，加锁快，并且不会产生死锁。其劣势在于：锁的粒度大，导致发生锁冲突的概率高，并发处理能力也因此较低。</div>
          </div>
          <div class="card-source">来源: 表锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">根据文档，InnoDB和MyISAM这两个存储引擎最主要的两个不同点是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">文档中指出的两个最大不同点是：第一，InnoDB支持事务（transaction）；第二，InnoDB默认采用行级锁。</div>
          </div>
          <div class="card-source">来源: 行锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">为什么文档中提到MyISAM存储引擎的表不会出现死锁（Deadlock）？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">因为MyISAM在执行SQL语句时，会一次性获得该语句所需要的全部锁，无论是自动加锁还是通过 `LOCK TABLES` 显式加锁。这种机制避免了因交叉请求锁而导致的死锁情况。</div>
          </div>
          <div class="card-source">来源: 如何加表锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">模式</div>
          <div class="card-question">在什么场景下需要为MyISAM表显式加锁？其主要目的是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">模式</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">通常在需要模拟事务操作，以实现对某一时间点多个表的一致性读取时，会为MyISAM表显式加锁。例如，当需要检查订单总表和订单明细表的金额合计是否相等时，加锁可以防止在查询过程中数据发生变化。</div>
          </div>
          <div class="card-source">来源: 如何加表锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">不同的MySQL存储引擎支持哪些类型的锁？请分别说明MyISAM、BDB和InnoDB的情况。</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">根据文档中的表格：<br>- MyISAM 支持表锁。<br>- BDB 支持表锁和页锁。<br>- InnoDB 支持行锁和表锁。</div>
          </div>
          <div class="card-source">来源: MySQL锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">在使用 `LOCK TABLES` 命令为MyISAM表加读锁时，添加 `local` 选项有什么作用？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">添加 `local` 选项的作用是，在满足MyISAM表并发插入条件的情况下，允许其他用户在被锁定的表尾部插入新的记录。</div>
          </div>
          <div class="card-source">来源: 如何加表锁</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
